//Page Object
const url = require('../../config/config.js')
Page({
    data: {

    },

    onLoad() {
        // 验证是不是过期了
        const header = {
            'content-type': 'application/json',
            'Cookie': "JSESSIONID=" + wx.getStorageSync("token") //读取cookie
        }
        wx.request({
            url: url.b.getUserById,
            data: { id: 2 },
            header: header,
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log(result)
            },
            fail: (err) => {
                console.log(err)
            },
            complete: () => {}
        });
    },
    //options(Object)
    onShow: function() {
        const token = wx.getStorageSync("token");
        const code = wx.getStorageSync('code');
        const user = wx.getStorageSync("user");
        const tokentime = wx.getStorageSync("tokentime");

        const newDate = Date.parse(new Date());
        // 计算相差秒数
        const second = parseInt((newDate - tokentime) / 1000);
        console.log(second);
        // 如果时间戳大于1800s,则跳转到登陆页面
        if (second > 1800) {
            wx.navigateTo({ url: '/pages/login/index' });
        };
        // 判断是不是登陆过期，
        // 判断有没有授权
        if (!wx.getStorageSync("userInfo").nickName) {

            wx.navigateTo({ url: '/pages/auth/index' });

        }
        // 权限设置
        console.log(user)
            // 首先判断是不是管理员根据type来判断
        if (user.type == 0) {
            console.log('客户')
            wx.navigateTo({ url: '/pages/custom/index?type=0' });
        } else if (user.type == 1) {
            wx.navigateTo({
                url: '/pages/supplier/index?type=1',
            });
            console.log('供应商')
        } else if (user.type == 2) {
            const adminType = user.adminType;
            console.log(adminType);
            switch (adminType) {
                case "-1":
                    console.log('老板登陆了');
                    break;
                case "0":
                    console.log('超管');
                    break;
                case "1":
                    wx.navigateTo({
                        url: "/pages/cutting/index?type=0",
                    });
                    break;
                case "2":
                    wx.navigateTo({
                        url: "/pages/cutting/index?type=1",
                    });
                    break;
                case "3":
                    wx.navigateTo({
                        url: "/pages/cutting/index?type=2",
                    });
                    break;
                case "4":
                    wx.navigateTo({
                        url: "/pages/cutting/index?type=3",
                    });
                    break;
                case "5":
                    wx.navigateTo({
                        url: "/pages/cutting/index?type=4",
                    });
                    break;
                case "5":
                    wx.navigateTo({
                        url: "/pages/storeHouse/index",
                    });
                    break;


            }

        }

    }
});